﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Utilities;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

namespace CryptographyTester
{
	class Program
	{
		static void Main(string[] args)
		{
			string sampleString = @"
“You ain’t gonna believe this, but you used to fit right here. (He gestures to the palm of his hand). I’d hold you up to say to your mother, ‘This kid’s gonna be the best kid in the world. This kid’s gonna be somebody better than anybody I ever knew.’ And you grew up good and wonderful. It was great just watchin’ you, every day was like a privilege. Then the time come for you to be your own man and take on the world, and you did. But somewhere along the line, you changed. You stopped being you. You let people stick a finger in your face and tell you you’re no good. And when things got hard, you started lookin’ for something to blame, like a big shadow.
Let me tell you something you already know.The world ain’t all sunshine and rainbows. It’s a very mean and nasty place, and I don’t care how tough you are, it will beat you to your knees and keep you there permanently if you let it. You, me, or nobody is gonna hit as hard as life. But it ain’t about how hard you hit, it’s about how hard you can get hit and keep moving forward. How much you can take and keep moving forward. That’s how winning is done!
Now if you know what you’re worth, then go out and get what you’re worth! But you gotta be willing to take the hits. And not pointing fingers saying you ain’t where you wanna be because of him, or her, or anybody! Cowards do that and that ain’t you! You’re better than that!
I’m always gonna love you no matter what. No matter what happens. You’re my son and you’re my blood. You’re the best thing in my life. But until you start believing in yourself, you ain’t gonna have a life.
Don’t forget to visit your mother.”
";
			byte[] sampleBytes = Encoding.ASCII.GetBytes(sampleString);
			byte[] junk = new byte[sampleBytes.Length];
			Array.Copy(sampleBytes, junk, sampleBytes.Length);

			byte[] key = Encoding.ASCII.GetBytes("tkiatkungwanglai");
			byte[] iv = Encoding.ASCII.GetBytes("katherine_beaman");

			string encryptedText = CryptographyHelper.SymmetricEncrypt(sampleString, key, iv);
			string decryptedEncryptedText = CryptographyHelper.SymmetricDecrypt(encryptedText, key, iv);
			Console.WriteLine(sampleString == decryptedEncryptedText);

			byte[] encryptedBytes = CryptographyHelper.SymmetricEncrypt(sampleBytes, key, iv);
			byte[] decryptedEncryptedBytes = CryptographyHelper.SymmetricDecrypt(encryptedBytes, key, iv);
			Console.WriteLine(sampleBytes.SequenceEqual(decryptedEncryptedBytes));

			sampleString = @"0123456789";
			sampleBytes = Encoding.ASCII.GetBytes(sampleString);

			using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
			{
				try
				{
					MemoryStream sampleStream = new MemoryStream(junk);

					encryptedText = CryptographyHelper.AsymmetricEncrypt(rsa, sampleString);
					decryptedEncryptedText = CryptographyHelper.AsymmetricDecrypt(rsa, encryptedText);
					Console.WriteLine(sampleString == decryptedEncryptedText);

					encryptedBytes = CryptographyHelper.AsymmetricEncrypt(rsa, sampleBytes);
					decryptedEncryptedBytes = CryptographyHelper.AsymmetricDecrypt(rsa, encryptedBytes);
					Console.WriteLine(sampleBytes.SequenceEqual(decryptedEncryptedBytes));
				}
				finally
				{
					rsa.PersistKeyInCsp = false;
				}
			}


			// Need to get a dummy certificate to test with.
			// Consider creating overloads in CryptographyHelper to include public
			// and private keys manually.

			Console.In.ReadLine();

		}
	}
}
